package com.sjgs.gis.web.rest;

import com.sjgs.gis.service.NodePluginService;
import com.sjgs.gis.web.errors.BadRequestAlertException;
import com.sjgs.gis.service.dto.NodePluginDTO;
import com.sjgs.gis.service.dto.NodePluginCriteria;
import com.sjgs.gis.service.NodePluginQueryService;

import io.github.jhipster.web.util.HeaderUtil;
import io.github.jhipster.web.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing {@link com.sjgs.gis.domain.NodePlugin}.
 */
@RestController
@RequestMapping("/api")
public class NodePluginResource {

    private final Logger log = LoggerFactory.getLogger(NodePluginResource.class);

    private static final String ENTITY_NAME = "geoserverportalNodePlugin";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final NodePluginService nodePluginService;

    private final NodePluginQueryService nodePluginQueryService;

    public NodePluginResource(NodePluginService nodePluginService, NodePluginQueryService nodePluginQueryService) {
        this.nodePluginService = nodePluginService;
        this.nodePluginQueryService = nodePluginQueryService;
    }

    /**
     * {@code POST  /node-plugins} : Create a new nodePlugin.
     *
     * @param nodePluginDTO the nodePluginDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new nodePluginDTO, or with status {@code 400 (Bad Request)} if the nodePlugin has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/node-plugins")
    public ResponseEntity<NodePluginDTO> createNodePlugin(@Valid @RequestBody NodePluginDTO nodePluginDTO) throws URISyntaxException {
        log.debug("REST request to save NodePlugin : {}", nodePluginDTO);
        if (nodePluginDTO.getId() != null) {
            throw new BadRequestAlertException("A new nodePlugin cannot already have an ID", ENTITY_NAME, "idexists");
        }
        NodePluginDTO result = nodePluginService.save(nodePluginDTO);
        return ResponseEntity.created(new URI("/api/node-plugins/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /node-plugins} : Updates an existing nodePlugin.
     *
     * @param nodePluginDTO the nodePluginDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated nodePluginDTO,
     * or with status {@code 400 (Bad Request)} if the nodePluginDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the nodePluginDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/node-plugins")
    public ResponseEntity<NodePluginDTO> updateNodePlugin(@Valid @RequestBody NodePluginDTO nodePluginDTO) throws URISyntaxException {
        log.debug("REST request to update NodePlugin : {}", nodePluginDTO);
        if (nodePluginDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        NodePluginDTO result = nodePluginService.save(nodePluginDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, nodePluginDTO.getId().toString()))
            .body(result);
    }

    /**
     * {@code GET  /node-plugins} : get all the nodePlugins.
     *
     * @param pageable the pagination information.
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of nodePlugins in body.
     */
    @GetMapping("/node-plugins")
    public ResponseEntity<List<NodePluginDTO>> getAllNodePlugins(NodePluginCriteria criteria, Pageable pageable) {
        log.debug("REST request to get NodePlugins by criteria: {}", criteria);
        Page<NodePluginDTO> page = nodePluginQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * {@code GET  /node-plugins/count} : count all the nodePlugins.
     *
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the count in body.
     */
    @GetMapping("/node-plugins/count")
    public ResponseEntity<Long> countNodePlugins(NodePluginCriteria criteria) {
        log.debug("REST request to count NodePlugins by criteria: {}", criteria);
        return ResponseEntity.ok().body(nodePluginQueryService.countByCriteria(criteria));
    }

    /**
     * {@code GET  /node-plugins/:id} : get the "id" nodePlugin.
     *
     * @param id the id of the nodePluginDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the nodePluginDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/node-plugins/{id}")
    public ResponseEntity<NodePluginDTO> getNodePlugin(@PathVariable Long id) {
        log.debug("REST request to get NodePlugin : {}", id);
        Optional<NodePluginDTO> nodePluginDTO = nodePluginService.findOne(id);
        return ResponseUtil.wrapOrNotFound(nodePluginDTO);
    }

    /**
     * {@code DELETE  /node-plugins/:id} : delete the "id" nodePlugin.
     *
     * @param id the id of the nodePluginDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/node-plugins/{id}")
    public ResponseEntity<Void> deleteNodePlugin(@PathVariable Long id) {
        log.debug("REST request to delete NodePlugin : {}", id);
        nodePluginService.delete(id);
        return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString())).build();
    }
}
